package com.yellowbook.dde.client;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.ws.rs.core.MediaType;
import javax.xml.bind.JAXBException;

import org.apache.commons.lang.builder.ToStringBuilder;
import org.apache.commons.lang.builder.ToStringStyle;

import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.WebResource;
import com.sun.jersey.api.client.config.ClientConfig;
import com.sun.jersey.api.client.config.DefaultClientConfig;
import com.yellowbook.common.utils.JAXBUtility;
import com.yellowbook.constant.DDEServiceParams;
import com.yellowbook.constant.types.LocationType;
import com.yellowbook.dde.jaxbs.AccountInfoData;
import com.yellowbook.dde.jaxbs.DealBean;
import com.yellowbook.dde.jaxbs.DealCategoryBean;
import com.yellowbook.dde.jaxbs.DealDataForGoogleOffer;
import com.yellowbook.dde.jaxbs.DealDataForNC;
import com.yellowbook.dde.jaxbs.DealFiltersBean;
import com.yellowbook.dde.jaxbs.DealTableBean;
import com.yellowbook.dde.jaxbs.DealsFeedData;
import com.yellowbook.dde.jaxbs.LocationBean;
import com.yellowbook.dde.jaxbs.MerchantBean;
import com.yellowbook.dde.jaxbs.RedemptionBean;

import junit.framework.TestCase;

public class TestDDEClient extends TestCase{
	/*
	public void testSave() throws JAXBException, IOException {
		ClientConfig cc = new DefaultClientConfig();
		cc.getClasses().add(DealBean.class);
		Client client = Client.create(cc);
		
		DealBean deal = new DealBean();
		deal.setTitle("title");
		DealDataForGoogleOffer dealData4GO = new DealDataForGoogleOffer();
		DealCategoryBean cate = new DealCategoryBean();
		cate.setName("cate");
		dealData4GO.setCategory(cate );
		deal.setGoogleDeal(dealData4GO );
		
		DealDataForNC dealData4NC = new DealDataForNC();
		dealData4NC.setAddReviews("reviews");
		dealData4NC.setCategory(cate);
		deal.setNcDeal(dealData4NC );
		
		AccountInfoData accountInfoData = new AccountInfoData();
		accountInfoData.setArtID("art");
		deal.setAccountInfo(accountInfoData );
		
		
		LocationBean redemptionLocationInfo = new LocationBean();
		redemptionLocationInfo.setAddress1("address1");
		redemptionLocationInfo.setCountryCode("US");
		redemptionLocationInfo.setPostalCode("MA");
		redemptionLocationInfo.setCity("Boston");
		redemptionLocationInfo.setLocationTypeID(LocationType.MERCHANT_LOCATION_TYPE_ID);
		MerchantBean merchantBean = new MerchantBean();
		merchantBean.setName("merchant");
		merchantBean.setMerchantContact(redemptionLocationInfo);
		deal.setMerchant(merchantBean );
		
		RedemptionBean redemption = new RedemptionBean();
		redemption.setInstructions("abc");
		deal.setRedemption(redemption );
		redemptionLocationInfo.setLocationTypeID(LocationType.REDEMPTION_LOCATION_TYPE_ID);
		deal.getRedemptionLocations().add(redemptionLocationInfo );
		System.out.println(JAXBUtility.toString(DealBean.class, deal));
		WebResource r = client.resource("http://172.20.230.27:8081/ddeservice/deal/save?apiKey=71b9662cc3d5fe6d4ae503bc36c19d34");
		DealFeedData d = r.queryParam("userID", "1").accept(MediaType.APPLICATION_XML_TYPE).type(MediaType.APPLICATION_XML_TYPE).post(DealFeedData.class, deal);
		System.out.println(d.getResponseCode());
	}
	*/
	public void testGetByFilter() {
		ClientConfig cc = new DefaultClientConfig();
		cc.getClasses().add(DealsFeedData.class);
		Client client = Client.create(cc);
	
		HashMap<String, String> map = new HashMap<String, String>();
		map.put(DDEServiceParams.PARAM_NAME_PAGE_NUM, "1");
		map.put(DDEServiceParams.PARAM_NAME_ROWS_PER_PAGE, "25");
		map.put(DDEServiceParams.PARAM_NAME_SORT_COL, "abc");
		WebResource r = client.resource("http://172.20.230.27:8081/ddeservice/deals?apiKey=71b9662cc3d5fe6d4ae503bc36c19d34");
		DealFiltersBean dealfilters = new DealFiltersBean();
		dealfilters.setTitle("title");
		DealsFeedData d = r.accept(MediaType.APPLICATION_XML_TYPE).type(MediaType.APPLICATION_XML_TYPE).post(DealsFeedData.class, dealfilters);
	
		DealTableBean ds = d.getDeals();
		print(ds.getDeals());
		System.out.println(ds.getTotalCount());
//		client.getGoogleOfferStatus(90);

	}
	
	private void print(DealBean deal) {
		System.out.println(ToStringBuilder.reflectionToString(deal, ToStringStyle.MULTI_LINE_STYLE));
	}
	
	private void print(List<DealBean> deals) {
		for(DealBean d : deals) {
			print(d);
		}
	}
	
	
}
